<template>
  <div class="app-container">
    <!-- 顶部查询和新增区域 -->
    <div class="top-operation-panel">
			<el-form ref="componentAnalysisRef" :model="form" :rules="rules" :inline="true" label-width="80px">
				<el-form-item label="组件名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入组件名称" clearable style="width: 150px"/>
				</el-form-item>
				<el-form-item label="组件类别" prop="componentType">
					<el-select v-model="form.componentType" placeholder="请选择" style="width: 120px">
						<el-option label="目标" value="目标" />
						<el-option label="组件" value="组件" />
						<el-option label="超系统组件" value="超系统组件" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button 
						:type="form.name.trim() ? 'primary' : 'default'"
						:disabled="!form.name || !form.name.trim()"
						icon="Plus" 
						@click="submitForm"
					> 添加 </el-button>
				</el-form-item>
			</el-form>
    </div>
    
    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="componentAnalysisList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" width="55" align="center" type="index" />
      <el-table-column label="组件名称" align="left" prop="name" show-overflow-tooltip/>
      <el-table-column label="组件类别" align="left" prop="componentType" show-overflow-tooltip/>
      <el-table-column label="组件描述" align="left" prop="description" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty />
      </template>
    </el-table>
    
    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改组件对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" draggable append-to-body>
      <el-form ref="dialogFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入组件名称" />
        </el-form-item>
        <el-form-item label="组件类别" prop="componentType">
          <el-select v-model="form.componentType" placeholder="请选择组件类别" style="width: 100%">
            <el-option label="目标" value="目标" />
            <el-option label="组件" value="组件" />
            <el-option label="超系统组件" value="超系统组件" />
          </el-select>
        </el-form-item>
        <el-form-item label="组件描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入组件描述" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialogForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ComponentAnalysis">
import { listComponentAnalysis, getComponentAnalysis, delComponentAnalysis, addComponentAnalysis, updateComponentAnalysis } from "@/api/business/componentAnalysis"
import { useRoute } from 'vue-router'

const route = useRoute()
const { proxy } = getCurrentInstance()

const componentAnalysisList = ref([])
const open = ref(false)
const loading = ref(true)
const total = ref(0)
const title = ref("")
const ids = ref([])

const data = reactive({
  form: {
    projectId: route.query.id,
    name: '',
    componentType: '组件',
    description: ''
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: route.query.id,
    name: null,
    componentType: null,
  },
  rules: {
    name: [
      { required: true, message: "组件名称不能为空", trigger: "blur" }
    ],
    componentType: [
      { required: true, message: "组件类别不能为空", trigger: "change" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询组件列表 */
function getList() {
  loading.value = true
  listComponentAnalysis(queryParams.value).then(response => {
    componentAnalysisList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置搜索条件 */
function resetQuery() {
  proxy.resetForm("queryFormRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    projectId: route.query.id,
    name: '',
    componentType: '组件',
    description: ''
  }
  proxy.resetForm("componentAnalysisRef")
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id
  getComponentAnalysis(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改组件"
  })
}

/** 顶部添加组件提交 */
function submitForm() {
  proxy.$refs["componentAnalysisRef"].validate(valid => {
    if (valid) {
      addComponentAnalysis(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功")
        reset()
        getList()
      })
    }
  })
}

/** 对话框表单提交 */
function submitDialogForm() {
  proxy.$refs["dialogFormRef"].validate(valid => {
    if (valid) {
      if (form.value.id) {
        updateComponentAnalysis(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _id = row.id
  proxy.$modal.confirm('是否确认删除该组件？').then(function() {
    return delComponentAnalysis(_id)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

// 初始化
getList()
</script>

<style scoped>
.top-operation-panel {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 2px;
  margin-bottom: 20px;
}
</style>